package com.shenmazong.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shenmazong.pojo.TbSeller;
import com.shenmazong.pojo.TbUser;
import com.shenmazong.service.TbSellerService;
import com.shenmazong.mapper.TbSellerMapper;
import com.shenmazong.utils.ResultResponse;
import com.shenmazong.utils.TokenUtils;
import com.shenmazong.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 *
 */
@Service
@Slf4j
public class TbSellerServiceImpl implements TbSellerService{

    @Autowired
    TbSellerMapper tbSellerMapper;

    @Autowired
    RedisTemplate redisTemplate;

    @Override
    public ResultResponse userLogin(LoginVo loginVo) {
        //-- 判断用户存不存在
        TbSeller tbSeller = tbSellerMapper.selectOne(new QueryWrapper<TbSeller>().lambda().eq(TbSeller::getMobile, loginVo.getUserName()).last("limit 1"));
        if(tbSeller == null) {
            return ResultResponse.FAILED(404, "用户不存在");
        }

        //-- 判断密码
        if(!tbSeller.getPassword().equals(loginVo.getPassWord())) {
            return ResultResponse.FAILED(403, "密码不正确");
        }

        //-- 生成token

        // token过期时间：redis的过期时间
        // 注意：token的过期时间建议比redis中要稍微长一点
        String tokenKey = TokenUtils.token()
                .setKey("123456")
                .setExpire(60 * 30)
                .createToken("userId", ""+tbSeller.getSellerId(), "userName", tbSeller.getMobile());

        //-- 把token放到redis
        // 问题1：token的key怎么设置：userId作为key：多终端的时候，是有问题：
        // key(seller:userid)=tokenKey(tokenKey)
        // tokenKey = realtoken

        // 问题2：token自动更新
        String realToken = tokenKey;
        String key = "seller:"+tbSeller.getSellerId();
        redisTemplate.opsForValue().set(key, tokenKey, 30, TimeUnit.MINUTES);
        redisTemplate.opsForValue().set(tokenKey, realToken, 30, TimeUnit.MINUTES);

        //-- 返回用户信息
        SellerInfoVo sellerInfoVo = new SellerInfoVo();
        BeanUtils.copyProperties(tbSeller, sellerInfoVo);
        sellerInfoVo.setToken(tokenKey);

        return ResultResponse.SUCCESS(sellerInfoVo);
    }

    @Override
    public ResultResponse userResetPassword(ResetPassworVo resetPassworVo) {
        // 获取用户
        TbSeller tbSeller = tbSellerMapper.selectById(resetPassworVo.getUserId());
        if(tbSeller == null) {
            return ResultResponse.FAILED(404, "用户不存在");
        }

        // 比较密码
        if(!tbSeller.getPassword().equals(resetPassworVo.getOldPass())) {
            return ResultResponse.FAILED(403, "密码不正确");
        }

        // 修改密码
        tbSeller.setPassword(resetPassworVo.getNewPass());
        tbSellerMapper.updateById(tbSeller);

        return ResultResponse.SUCCESS();
    }

    @Override
    public ResultResponse addUser(AddSellerVo addSellerVo) {
        // 判断用户名是否存在(判断手机号)
        TbSeller tbSeller = tbSellerMapper.selectOne(new QueryWrapper<TbSeller>().lambda().eq(TbSeller::getMobile, addSellerVo.getMobile()));
        if(tbSeller != null) {
            return ResultResponse.FAILED(410, "商家已存在");
        }

        // 开始注册
        TbSeller seller = new TbSeller();
        BeanUtils.copyProperties(addSellerVo, seller);
        seller.setCreateTime(new Date());

        tbSellerMapper.insert(seller);

        return ResultResponse.SUCCESS();
    }

    @Override
    public ResultResponse sendCode(MobileVo mobileVo) {
        // 生成随机验证码
        String code = "1234";

        // 验证码存入redis
        redisTemplate.opsForValue().set(mobileVo.getMobile(), code, 1, TimeUnit.MINUTES);

        // 调用短信发送接口
        log.info("code:"+code);

        return ResultResponse.SUCCESS();
    }

    @Override
    public ResultResponse getSellerInfo(IdVo idVo) {

        TbSeller tbSeller = tbSellerMapper.selectById(idVo.getId());
        if(tbSeller == null) {
            return ResultResponse.FAILED(404, "商家不存在");
        }

        SellerInfoVo sellerInfoVo = new SellerInfoVo();
        BeanUtils.copyProperties(tbSeller, sellerInfoVo);

        return ResultResponse.SUCCESS(sellerInfoVo);
    }

    @Override
    public ResultResponse setSellerInfo(SellerInfoVo sellerInfoVo) {
        // 判断用户存不存在
        TbSeller tbSeller = tbSellerMapper.selectById(sellerInfoVo.getSellerId());
        if(tbSeller == null) {
            return ResultResponse.FAILED(404, "商家不存在");
        }

        // 商家信息修改
        BeanUtils.copyProperties(sellerInfoVo, tbSeller);
        tbSellerMapper.updateById(tbSeller);

        return ResultResponse.SUCCESS(sellerInfoVo);
    }
}




